#include <DataTime.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>

DataTime* DataTime::instance = new DataTime();

DataTime::DataTime(/* args */)
{
    firstTrigger = true;
    mTimedPeriod = 60;
    // TimerListener::getInstance()->registeredTimer(this, nullptr, mTimedPeriod, 1);
    #ifdef TIME_SELECT_ENABLE
    TimerListenerThread::getInstance()->registeredTimer(this, nullptr, 1, 1);
    #else
    // TimerListener::getInstance()->registeredTimer(this, nullptr, 1, 1);
    #endif

    // /* 创建一个差选Rtc是否正常运行的线程 */
    rtcThread = new RtcThread(this);
    // rtcThread->start();
}

DataTime::~DataTime()
{
    if (rtcThread != nullptr) {
        delete rtcThread;
    }
}

DataTime* DataTime::getInstance()
{
    return instance;
}

bool DataTime::setDateTime(unsigned int year, unsigned int month, unsigned day, unsigned int hour, unsigned int min, unsigned int sec)
{
    if (year >= 1900 && month >= 1 && month <= 12 && day >= 1 && day <= 31 && hour >= 0 && hour <= 23 && min >= 0 && min <= 59 && sec >= 0 && sec <= 59) {
        mYear = year;
        mMonth = month;
        mDay = day;
        mHour = hour;
        mMin = min;
        mSec = sec;
        // SystemController::getInstance()->setDateTime(mYear, mMonth, mDay, mHour, mMin, mSec);
        // log_string = DataTime::getInstance()->getTimeString();
        systemNoBlock("hwclock -w");/* 同步系统时间到RTC */
        return true;
    }
    return false;
}

bool DataTime::setDateTime(unsigned int hour, unsigned int min)
{
    if (hour >= 0 && hour <= 23 && min >= 0 && min <= 59) {
        mHour = hour;
        mMin = min;
        mSec = 0;
        setDateTime(mYear, mMonth, mDay, mHour, mMin, mSec);
        return true;
    }
    return false;
}

bool DataTime::setTimedPeriod(int timedPeriod) 
{
    // firstTrigger = true;
    // mTimedPeriod = timedPeriod;
    // return TimerListener::getInstance()->updateTimer(this, mTimedPeriod, 1);
    return true;
}

bool DataTime::rtcIsRunning()
{
    bool ret = true;
    std::string str1 = "08:02:01";
    if (str1.compare("err") == 0) {
        SIGNWAY_LOGI("rtc is err");
        return false;
    }
    usleep(1200 * 1000);
    std::string str2 = "08:02:03";
    if (str1.compare(str2) == 0) {
        SIGNWAY_LOGI("rtc is ok but no run");
        return false;
    }
    SIGNWAY_LOGI("rtc is ok");
    return true;
}

bool DataTime::isLeapYear(){
    return (mYear % 4 == 0 && mYear % 100 != 0) || (mYear % 400 == 0);
}

bool DataTime::isMonthEnd()
{
    int month[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    if (isLeapYear() && mMonth == 2) {
        return mDay == 29 + 1;
    } else {
        return mDay == (month[mMonth - 1] + 1);
    }
}

void DataTime::syncTime(void)
{
    if (rtcThread != nullptr) {
        rtcThread->start(0x0000001F);
        rtcThread->start(0x0000002F);
        // rtcThread->stop();
        // rtcThread->stop();
    }
}

std::string DataTime::getTimeString(void)
{
    std::string ret;
    char timeStr[32];
    time_t tim;
    struct tm *p_tm = NULL;
    time(&tim);
    p_tm = localtime(&tim);
    snprintf(timeStr, 32, "%02d:%02d", p_tm->tm_hour, p_tm->tm_min);
    ret += timeStr;
    return ret;
}

void DataTime::timer_log()
{
    
}

void DataTime::addOneScond()
{
    // if (BaseActivity::machineState != OFF_STATE) {
        // timer_log();
        // NotifyQueueManager::getInstance()->sendNotify(SYS_DATE_TIME_CHANGED_SECOND, nullptr);
    // }
    mSec++;
    if (mSec == 60) {
        mSec = 0;
        mMin++;
        // log_string = DataTime::getInstance()->getTimeString();//1min更新一次
    }
    if (mMin == 60) {
        mMin = 0;
        mHour++;
        rtcThread->start();
        // MemaryThread::getInstance()->synchronizeTheKernelLog(); /* 内核日志同步到SDCARD下面 */
    }
    if (mHour == 24) {
        mHour = 0;
        mDay++;
    }
    if (isMonthEnd()) {
        mDay = 0;;
        mMonth++;
    }
    if (mMonth == 13) {
        mMonth = 1;
        mYear++;
    }
}

void DataTime::timerTrigger(void* arg)
{
    // if (MainApplication::within_10SOfBooting) {
    //     static int timer = 0;
    //     timer++;
    //     if (timer == 20) {
    //         MainApplication::within_10SOfBooting = false;
    //     }
    // }
    addOneScond();
}

